﻿using System;
using System.Collections.Generic;
using System.Linq;
//using System.Threading;
using System.Web;
using DrillOnline.Hubs;
using Microsoft.AspNet.SignalR;
using DrillOnline.Services.Scenarios;

namespace DrillOnline.Services
{

    public class DrillScenarios : Scenarios.Scenarios<DrillScene>
    {
        public DrillOnline.Models.DrillSubject Subject { get; set; }
        public DrillOnline.Models.TrainingSession Session { get; set; }
        bool _IsReady = false;

        public bool IsReady
        {
            get { return _IsReady; }
            set
            {
                if (_IsReady != value) { _IsReady = value; NotifyStatusChange(); }
            }
        }
        private DrillHubUtility hubUtility;
        public List<UserMessages> Messages { get; set; }

        public DrillScenarios()
            : base()
        {
            hubUtility = new DrillHubUtility();
            Messages = new List<UserMessages>();

        }

        public override void BroadcastUpdatedScenes(ActorGroup g, IList<DrillScene> sl)
        {
            hubUtility.BroadcastUpdatedScenes(g, sl, this);
        }

        public override void NotifySettingChange()
        {
            hubUtility.NotifySettingChange(this);
        }
        public override string ScenariosName
        {
            get
            {
                return Subject.Title;
            }
            set
            {
                Subject.Title = value;
            }
        }


        public override void NotifyStatusChange()
        {
            hubUtility.NotifyStatusChange(this);
        }
        public override void NotifyPlayIndexChange(int oldidx)
        {
            hubUtility.NotifyPlayIndexChange(oldidx,this);
        }
        public void BroadcastMessage(string fromuser,string flag, string fromuser_group, string[] targetGroups,  string content,string msgtype,string targetEventID,Microsoft.AspNet.SignalR.Hubs.HubCallerContext context=null)
        {
            var m = new UserMessages()
            {
                RealTime = Timeline.RealCurrentTime,
                StoryTime = Timeline.StoryCurrentTime,
                PlayingOffset = Timeline.PlayingOffset,
                FromActor = fromuser,
                FromActor_Group = fromuser_group,
                TargetGroup = targetGroups,
                Content = content,
                MsgType = msgtype,
                TargetEventID = targetEventID
            };
            Messages.Add(m);

            hubUtility.BroadcastMessage(this, fromuser, flag, fromuser_group, targetGroups, content, msgtype, targetEventID,context);
        }

    }
    public class UserMessages
    {
        public DateTime RealTime { get; set; }
        public DateTime StoryTime { get; set; }
        public double PlayingOffset { get; set; }
        public string Title { get; set; }
        public string FromActor { get; set; }
        public string FromActor_Group { get; set; }
        public string[] TargetGroup { get; set; }
        public string Content { get; set; }
        public string MsgType { get; set; }
        public string TargetEventID { get; set; }
        public int MessagePoint { get; set; }
    }
}